import { useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import type { MenuProps } from 'antd';
import {
  FileTextOutlined,
  SendOutlined,
  CommentOutlined,
  OrderedListOutlined,
  UserOutlined,
  PlusOutlined,
  SettingOutlined,
  DashboardOutlined,
  TeamOutlined,
  BookOutlined,
} from '@ant-design/icons';

type MenuItem = Required<MenuProps>['items'][number];

// 用户端菜单项
const userProblemNavItems: MenuItem[] = [
  {
    label: '题目列表',
    key: '/problem/list',
    icon: <FileTextOutlined />,
  },
  {
    label: '题单管理',
    key: '/problem/problem-sets',
    icon: <BookOutlined />,
  },
  {
    key: '/question/list',
    icon: <SendOutlined />,
    label: '问题列表',
  },
  {
    key: '/discussion/list',
    icon: <CommentOutlined />,
    label: '讨论',
  },
];

const userContestNavItems: MenuItem[] = [
  {
    key: '/contest/list',
    icon: <OrderedListOutlined />,
    label: '比赛列表',
  },
];

// userExamNavItems: 已移除（exam模块已删除）

// 管理员端菜单项
const adminProblemNavItems: MenuItem[] = [
  {
    label: '题目列表',
    key: '/admin/problem/list',
    icon: <FileTextOutlined />,
  },
  {
    label: '创建题目',
    key: '/admin/problem/create',
    icon: <PlusOutlined />,
  },
  {
    label: '题单管理',
    key: '/admin/problem/problem-sets',
    icon: <BookOutlined />,
  },
];

const adminContestNavItems: MenuItem[] = [
  {
    key: '/admin/contest/list',
    icon: <OrderedListOutlined />,
    label: '比赛列表',
  },
  {
    key: '/admin/contest/create',
    icon: <PlusOutlined />,
    label: '创建比赛',
  },
];

// adminExamNavItems: 已移除（exam模块已删除）

const adminQuestionNavItems: MenuItem[] = [
  {
    key: '/admin/question/list',
    icon: <OrderedListOutlined />,
    label: '问题列表',
  },
];

const adminUserNavItems: MenuItem[] = [
  {
    key: '/admin/user/list',
    icon: <UserOutlined />,
    label: '用户列表',
  },
  {
    key: '/admin/user/teams',
    icon: <TeamOutlined />,
    label: '团队管理',
  },
];

const adminDashboardNavItems: MenuItem[] = [
  {
    key: '/admin/',
    icon: <DashboardOutlined />,
    label: '控制台首页',
  },
  {
    key: '/admin/setting/',
    icon: <SettingOutlined />,
    label: '系统设置',
  },
];

const adminPaperNavItems: MenuItem[] = [
  {
    key: '/admin/paper/list',
    icon: <OrderedListOutlined />,
    label: '试卷列表',
  },{
    key: '/admin/paper/create',
    icon: <PlusOutlined />,
    label: '创建试卷',
  },
];


interface SubNavResult {
  subNavItems: MenuProps['items'];
  selectedKey: string;
}

export const useSubNav = (): SubNavResult => {
  const location = useLocation();
  const pathname = location.pathname;
  
  return useMemo(() => {
    // 管理员端路由
    if (pathname.startsWith('/admin')) {
      if (pathname.startsWith('/admin/problem')) {
        return {
          subNavItems: adminProblemNavItems,
          selectedKey: pathname,
        };
      } else if (pathname.startsWith('/admin/contest')) {
        return {
          subNavItems: adminContestNavItems,
          selectedKey: pathname,
        };
      } else if (pathname.startsWith('/admin/question')) {
        return {
          subNavItems: adminQuestionNavItems,
          selectedKey: pathname,
        };
      } else if (pathname.startsWith('/admin/user')) {
        return {
          subNavItems: adminUserNavItems,
          selectedKey: pathname,
        };
      } else if(pathname.startsWith('/admin/paper')){
        return {
          subNavItems: adminPaperNavItems,
          selectedKey: pathname,
        };
      } else {
        // 管理控制台默认菜单
        return {
          subNavItems: adminDashboardNavItems,
          selectedKey: pathname,
        };
      }
    } 
    
    // 用户端路由
    if (pathname.startsWith('/problem') || pathname.startsWith('/question') || pathname.startsWith('/discussion')) {
      return {
        subNavItems: userProblemNavItems,
        selectedKey: pathname,
      };
    } else if(pathname.startsWith('/contest')) {
      return {
        subNavItems: userContestNavItems,
        selectedKey: pathname,
      };
    } 
    
    return {
      subNavItems: [],
      selectedKey: pathname,
    };
  }, [pathname]);
}; 